/** This is a script used for jade custom build file. It's purpose is to extract the build
 * parameters and construct the required command line argument for jade
 */

AGENT_SEPARATOR = ";"
AGENT_CLASS_SEPARATOR = ":";

function echo(message) {
	echoObject = project.createTask("echo");
	echoObject.setMessage(message);
	echoObject.perform();
}

function getValues(keyName, separator) {
	try {
		result = project.getProperty(keyName).split(separator);
		return result;
	} catch(e) {
		echo("Parameter " + keyName + " not found in project build file");
	}
}

function checkArguments(agentNames, agentClasses, agentArguments) {
	if((agentNames.length == agentClasses.length) && 
	   (agentClasses.length == agentArguments.length)) {
	   //check the existance of each class in the class path
	   return true;
	} else {
		echo("The length of the arguments is invalid");
		return false;
	}
}

function buildArguments(agentNames, agentClasses, agentArguments) {
	idx = 0;
	result = "-agents " +  buildArgument(agentNames[idx], agentClasses[idx], 
										agentArguments[idx]);
	idx = 1;
	while(idx < agentNames.length) {
		result += AGENT_SEPARATOR + buildArgument(agentNames[idx], agentClasses[idx], 
												  agentArguments[idx]);
		idx++;
	}
	return result;
}

function buildArgument(agentName, agentClass, agentArguments) {
	return agentName + AGENT_CLASS_SEPARATOR + agentClass + "(" + agentArguments + ")";
}

function main() {
	agentNames = getValues("jade.names", ",");
	agentClasses = getValues("jade.classes", ",");
	agentArguments = getValues("jade.arguments", ";");
	cmd = buildArguments(agentNames, agentClasses, agentArguments);
	project.setProperty("jade.cmdLine", cmd);
	echo(cmd);
}

/* Call main as the entry point in the script. */
main();